﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Itop.YHPC.Model;
using System.Collections.Generic;
using Itop.YHPC.controller;

namespace Itop.YHPC.workflow
{
    public partial class workflow_tasklist_ajax : AjaxPage<yhpcMain>
    {
        
        int total = 0;
        protected override IList<yhpcMain> onRead()
        {
            string url = Request.Url.ToString();
            
            string wfid = Request.QueryString["wfid"];
            if (!BasePage.CheckProg(wfid)) {
                throw new Exception("你没有相关模块的权限!");
            }
            int page=int.Parse(Request.QueryString["page"]);
            int limit=int.Parse(Request.QueryString["limit"]);
            int i_wfid = int.Parse(wfid);
            IList<yhpcMain> list;
            if (BasePage.GetUser().LoginID == "admin") {
                if (i_wfid == 200 || i_wfid == 201 || i_wfid == 202)
                    list = workflowController.GetMainList("where zlflag='" + wfid + "' and ypgdj<>'非隐患' and pgdj<>'非隐患' order by fxrq desc");
                else
                    list = workflowController.GetMainList("where lcflag='" + wfid + "' and ypgdj<>'非隐患' and pgdj<>'非隐患'  order by fxrq desc");
            } else {
                if (wfid == "101" || wfid == "100")
                    list = workflowController.GetMainList("where lcflag='" + wfid + "' and tjr='" + (Session["user"] as sysuser).UserName + "' and ypgdj<>'非隐患' and pgdj<>'非隐患'  order by fxrq desc");
                else if (i_wfid == 105 || i_wfid == 106)
                    list = workflowController.GetMainList("where lcflag='" + wfid + "' and gsbm='" + GetGSBM() + "' and ypgdj<>'非隐患' and pgdj<>'非隐患'  order by fxrq desc");
                else if (i_wfid == 200)
                    list = workflowController.GetMainList("where zlflag='" + wfid + "' and ((zldw='' and yhdw='" + getdw() + "') or zldw='" + getdw() + "' ) and ypgdj<>'非隐患' and pgdj<>'非隐患'  order by fxrq desc");
                else if (i_wfid == 201)
                    list = workflowController.GetMainList("where zlflag='" + wfid + "' and yszzdw='" + getdw() + "' and ypgdj<>'非隐患' and pgdj<>'非隐患'  order by fxrq desc");
                else if (i_wfid == 102 || i_wfid == 103 || i_wfid == 104)
                    list = workflowController.GetMainList("where lcflag='" + wfid + "' and fxrdw='"+getdw()+"' and ypgdj<>'非隐患' and pgdj<>'非隐患'  order by fxrq desc");
                else
                    list = workflowController.GetMainList("where lcflag='" + wfid + "' and ypgdj<>'非隐患' and pgdj<>'非隐患'  order by fxrq desc");
            }
            PreList(ref list, wfid);
            total = list.Count;
            return GetPageList(page, list, limit);

        }
        private string getdw() {
            string dw = BasePage.GetUserDw().dwOwner;

            return dw;

        }
        protected override int GetTotal()
        {
            return total;
        }

        public IList<yhpcMain> GetPageList(int page, IList<yhpcMain> list, int limit)
        {
            IList<yhpcMain> temp = new List<yhpcMain>();
            for (int i = (page - 1) * limit; i < ((page * limit) > list.Count ? list.Count : (page * limit)); i++)
            {
                temp.Add(list[i]);
            }
            return temp;
        }

        protected override bool onCreate(string json)
        {
            yhpcMain main = Newtonsoft.Json.JsonConvert.DeserializeObject<yhpcMain>(json);
            int i_wfid = int.Parse(main.bhflag);
            if (i_wfid >= 100 && i_wfid <= 108)
            {
                if (i_wfid == 100 || i_wfid==101)
                    main.gsbm = getgsbm(main.yhzyfl);
                main.lcstatus = GetLCStatus(main.lcflag);
                main.lcflag = workflowController.GetWf(main.lcflag).url1;
                CreateNewRst(main);
            }
            else if (i_wfid == 200 || i_wfid == 201 || i_wfid == 202)
            {
                main.zlflag = workflowController.GetWf(main.zlflag).url1;
                main.zlstatus = GetLCStatus(main.zlflag);
                if (i_wfid == 201  && main.yssfxc == "否")
                {
                    main.zlstatus = "退回，待治理";
                    main.zlflag = "200";
                }
            }
            yhpcMain bExist = workflowController.GetMain(main.id);
            if (bExist == null)
            {
                main.tjr = (Session["user"] as sysuser).UserName;
               // main.sfzykz = (main.sfzykz == "on" ? "是" : "否");
                main.tjrq = DateTime.Now;
                main.tjflag = Request.QueryString["wfid"];
                main.zlflag = "200";
                main.zlstatus = "待治理";
                main.lcrecord = string.Format("{0}于{1}[{2}]", main.tjr, main.tjrq.ToString("yyyy-MM-dd hh:mm:ss"), GetLCName(main.bhflag));
                workflowController.Insert(main);
            }
            else
            {
                main.lcrecord = main.lcrecord + "->" + string.Format("{0}于{1}[{2}]", (Session["user"] as sysuser).UserName, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), GetLCName(main.bhflag));
                if (main.ypgdj == "非隐患" || main.pgdj == "非隐患")
                    main.lcrecord += "-非隐患";
                main.dqyh = "";
                main.dqyhsj = new DateTime(1900, 1, 1);
                workflowController.Update(main);
            }
            return true;
        }
        /// <summary>
        /// 根据专业查找部门,在新建的时候
        /// </summary>
        /// <param name="zyfl"></param>
        /// <returns></returns>
        private string getgsbm(string zyfl) {
            string gsdw = "";
            IList<yhpcDictionary> list= yhpcDictionaryController.GetList("where type='专业分类' and text='" + zyfl + "'");
            if (list.Count > 0)
                gsdw = list[0].Remark;
            return gsdw;
        }
        public void CreateNewRst(yhpcMain main)
        {
            if (workflowController.GetDictList(" where type='可能导致后果' and text='" + main.knhg + "'").Count == 0)
            {
                yhpcDictionary dict = new yhpcDictionary();
                dict.type = "可能导致后果";
                dict.text = main.knhg;
                dict.orderID = workflowController.GetMaxOrderid("可能导致后果");
                yhpcDictionaryController.Insert(dict);
            }
        }

        public string GetLCStatus(string lcflag)
        {
            yhpcWf wf = workflowController.GetWf(lcflag);
            return wf == null ? "" : wf.mc;
        }

        public string GetLCName(string lcflag)
        {
            sysprog prog = workflowController.GetProgList(" where id='"+lcflag+"'")[0];
            return prog.ProgName;
        }

        public string GetGSBM()
        {
            IList<yhpcOrg> list = yhpcOrgController.GetList(" where id=(select dwid from sysuser where loginid='" + (Session["user"] as sysuser).LoginID + "')");
            if (list.Count > 0)
                return list[0].dwOwner;
            else
                return "";
        }

        public string GetGSBMByFL(string fl)
        {
            IList<yhpcDictionary> list = yhpcDictionaryController.GetList(" where type='专业分类' and text='" + fl + "'");
            if (list.Count > 0)
                return list[0].Remark;
            else
                return "";
        }

        public void PreList(ref IList<yhpcMain> list, string wfid)
        {
            foreach (yhpcMain main in list)
            {
                switch (wfid)
                {
                    case "102":
                        main.ypgfzr = (Session["user"] as sysuser).UserName;
                        main.ypgrq1 = DateTime.Now;
                        main.gsbm = GetGSBMByFL(main.yhzyfl);
                        break;
                    case "103":
                        main.ypgsh2 = (Session["user"] as sysuser).UserName;
                        main.ypgrq2 = DateTime.Now;
                        break;
                    case "104":
                        main.ypgsh3 = (Session["user"] as sysuser).UserName;
                        main.ypgrq3 = DateTime.Now;
                        break;
                    case "105":
                        main.pgfzr = (Session["user"] as sysuser).UserName;
                        if (main.zlrq1.Year == 1900)
                            main.zlrq1 = main.zlrq2 = DateTime.Now.Date;
                        main.pgrq1 = DateTime.Now.Date;
                        if (main.gsbm == "" || main.gsbm==null)
                            main.gsbm = GetGSBMByFL(main.yhzyfl);
                        break;
                    case "106":
                        main.pgsh2 = (Session["user"] as sysuser).UserName;
                        main.pgrq2 = DateTime.Now;
                        break;
                    case "107":
                        main.pgsh3 = (Session["user"] as sysuser).UserName;
                        main.pgrq3 = DateTime.Now;
                        break;
                    case "108":
                        main.pgsh4 = (Session["user"] as sysuser).UserName;
                        main.pgrq4 = DateTime.Now;
                        break;
                    case "200":
                        // main.ypgsh2 = "admin";//Session["user"]
                        //main.ypgrq2 = DateTime.Now;
                        main.yssqrq = DateTime.Now;
                        break;
                    case "201":
                        // main.ypgsh2 = "admin";//Session["user"]
                        main.ysrq = DateTime.Now;
                        main.yszzdw = (main.yszzdw == "" ? main.zldw : main.yszzdw);
                        main.yssfxc = "";
                        //main.yssqdw = main.zldw;
                        main.yssqfzr = (main.yssqfzr == "" ? main.zlr : main.yssqfzr);
                        main.ysjl = "验收合格";
                        break;
                   
                }
            }
        }
    }
}
